package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.CashBack;
import ysq.xyg.model.Custom;
import ysq.xyg.model.Store;
import ysq.xyg.model.UseDo;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.model.info.CashBackInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.CashBackService;
import ysq.xyg.service.CustomService;
import ysq.xyg.service.StoreService;
import ysq.xyg.service.UseDoService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/cashBack")
public class CashBackController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(CashBackController.class);
    @Resource
    private CashBackService cashBackService;
    @Resource
    private CustomService customService;
    @Resource
    private UseDoService useDoService;
    @Resource
    private StoreService storeService;
    private CashBack cashBack;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView("/cashBackList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList(CashBack cashBack, Page page) throws MalformedURLException, UnsupportedEncodingException {
        cashBack.setRemoveTag("N");
        PageInfo<CashBack> pageInfo=cashBackService.pageLeft(cashBack,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        jsonObject.put("data",pageInfo.getList());
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(HttpServletRequest request,String id){
        ModelAndView view=new ModelAndView("/cashBackUpdateOrSave");
        //1、总管理员，2、财务号，3、店铺管理员
        int kind=acceptCurrentUserKind(request);
        view.addObject("kind",kind);
        List<Store> storeList=storeService.list(new Store());
        List<Custom> customList=customService.list(new Custom());
        CashBack cashBack=cashBackService.selectByPrimaryKey(id);
        view.addObject("customList",customList);
        view.addObject("storeList",storeList);
        view.addObject("cashBack",cashBack);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject UpdateOrAddEntity(HttpServletRequest request,CashBackInfo cashBackInfo){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(cashBackInfo.getId()==null || "".equals(cashBackInfo.getId())){
            cashBack=new CashBack();
            cashBack.setId(BaseUtil.uId());
            cashBack.setCreateTime(new Date());
            cashBack.setCustomId(cashBackInfo.getCustomId());
            cashBack.setWangWang(cashBackInfo.getWangWang());
            cashBack.setOrderNum(cashBackInfo.getOrderNum());
            cashBack.setWeChat(cashBackInfo.getWeChat());
            cashBack.setMoney(cashBackInfo.getMoney());
            cashBack.setReason(cashBackInfo.getReason());
            cashBack.setSkm(cashBackInfo.getSkm());
            cashBack.setUpdateTime(new Date());
            cashBack.setRemoveTag("N");
            cashBack.setBackMoney(cashBackInfo.getBackMoney());
            if(StringUtil.isNotEmpty(cashBackInfo.getReturnDate())){
                cashBack.setReturnTime(DateUtil.convertStringToDate(cashBackInfo.getReturnDate()));
            }
            cashBack.setReturnBank(cashBackInfo.getReturnBank());
            cashBack.setRemark(cashBackInfo.getRemark());
            cashBack.setMonth(cashBackInfo.getMonth());
            int id=cashBackService.insert(cashBack);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"添加返现信息["+cashBack.toString()+"]",BaseConstant.USE_DO_TYPE_ADD_CASHBACK));
            }
        }else{
            cashBack=cashBackService.selectByPrimaryKey(cashBackInfo.getId());
            cashBack.setCustomId(cashBackInfo.getCustomId());
            cashBack.setWangWang(cashBackInfo.getWangWang());
            cashBack.setOrderNum(cashBackInfo.getOrderNum());
            cashBack.setWeChat(cashBackInfo.getWeChat());
            cashBack.setMoney(cashBackInfo.getMoney());
            cashBack.setReason(cashBackInfo.getReason());
            cashBack.setSkm(cashBackInfo.getSkm());
            cashBack.setUpdateTime(new Date());
            cashBack.setBackMoney(cashBackInfo.getBackMoney());
            if(StringUtil.isNotEmpty(cashBackInfo.getReturnDate())){
                cashBack.setReturnTime(DateUtil.convertStringToDate(cashBackInfo.getReturnDate()));
            }
            cashBack.setReturnBank(cashBackInfo.getReturnBank());
            cashBack.setRemark(cashBackInfo.getRemark());
            cashBack.setMonth(cashBackInfo.getMonth());
            int id=cashBackService.updateByPrimaryKey(cashBack);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"修改返现信息["+cashBack.toString()+"]",BaseConstant.USE_DO_TYPE_UPDATE_CASHBACK));
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject DeleteEntity(HttpServletRequest request,String id){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            CashBack cashBack=cashBackService.selectByPrimaryKey(id);
            cashBack.setRemoveTag("Y");
            int num=cashBackService.updateByPrimaryKey(cashBack);
            if(num <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }else{
                useDoService.insert(new UseDo(baseUserInfo.getUserid(),"删除返现信息["+cashBack.toString()+"]",BaseConstant.USE_DO_TYPE_DELETE_CASHBACK));
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/export")
    public void getExport(HttpServletResponse response, CashBack cashBack){
        cashBack.setRemoveTag("N");
        cashBackService.export(response,cashBack);
    }
}
